<?php
class Base_Model extends CI_Model
{
    public function __construct(){
        parent::__construct();
    }


    public function img($img='')
    {
        if(!empty($img))
        {
            return "http://www.wayatrip.com".$img;
        }
        return '';
    }
    
    public function getUserInfoById($uid)
    {
        $key_info = array("telActive","userIcon", "identityActive", "active", 'telCode', 'telNum', 'identityErea', 'identityName', 'identityPicture', 'email', 'failMsg', 'utype');
        $key_user_info = array('userAge', 'nationality', 'language', 'introduction', 'resideCity', 'resideTime', 'country', 'code', 'rcity', 'addr', 'userGender');


        $query = $this->db->query("select * from user_v2 as u1 LEFT JOIN user_v2_userinfo as u2 on u1.uid = u2.uid  WHERE u1.uid ={$uid}");
        $userInfo = $query->row_array();

        $baseInfo = array();
        foreach ($key_user_info as $v) {
            $baseInfo[$v] = empty($userInfo[$v]) ? '' : $userInfo[$v];
        }
        foreach ($key_info as $v) {
            $baseInfo[$v] = empty($userInfo[$v]) ? '' : $userInfo[$v];
        }

        if ($baseInfo['identityName'] == '留学生') {
			if(stripos($baseInfo['identityErea'],'-|-'))
			{
			$data = explode('-|-', $baseInfo['identityErea']);
            if(isset($data[0]))
            $baseInfo['major'] = $data[0];
            else 
            $baseInfo['major'] = '';
            
            if(isset($data[1]))
            	$baseInfo['school'] = $data[1];
            else
            	$baseInfo['school'] = '';
            }
            $baseInfo['job'] = '';          
        }else{
        	$baseInfo['major'] = '';
        	$baseInfo['school'] = '';
        	$baseInfo['job'] = $baseInfo['identityErea'];
        }
        unset($baseInfo['identityErea']);
        $lang = array();
        foreach (explode(',', $userInfo['language']) as $v) {
            if ($v == 1) {
                $lang[] = '中文';
            } elseif ($v == 2) {
                $lang[] = '法文';
            } elseif ($v == 3) {
                $lang[] = '英文';
            }
        }
        $baseInfo['language'] = $lang;

        $userName = $userInfo['userFamily'].$userInfo['userName'];
        $baseInfo['realName'] = $userName;

        return $baseInfo;
    }


    public function exchange_rate($type="EUR")
    {
        $this->load->driver('cache',array('adapter' => 'apc', 'backup' => 'file'));
	    $exchange_rate=$this->cache->get('exchange_rate');
        if (empty($exchange_rate)) {
            $url = "http://apis.haoservice.com/lifeservice/exchange/rmbquot?key=100722bd81a64c3fb5985c525a51b913";
            $exchange_rate = file_get_contents($url);
            if ($exchange_rate) {
                $exchange_rate = json_decode($exchange_rate, true);
			    $this->cache->save('exchange_rate', $exchange_rate,21600);
			}
        }
        foreach ($exchange_rate['result'] as $v) {
            if ($v['code'] == $type) {
                return $v;
            }
        }
    }
    
    public function current_weather($city)
    {
    	$this->load->driver('cache',array('adapter' => 'apc', 'backup' => 'file'));
    	$current_weather=$this->cache->get('current_weather_'.$city);
    	if (empty($current_weather)) {
    		$url = "http://api.openweathermap.org/data/2.5/weather?APPID=83d2ceaaa3b466e6a1255f2c37ec1327&q=".$city;
    		$current_weather = file_get_contents($url);
    		if ($current_weather) {
    			$current_weather = json_decode($current_weather, true);
    			$this->cache->save('current_weather_'.$city, $current_weather,3600);
    		}
    	}

     	if ($current_weather['cod'] == 200) {
     		$temp=$current_weather['main']['temp']-273.15;
     		$temp=round($temp,1);
     		$icon=$current_weather['weather'][0]['id'];
     		
     		if($icon>199&&$icon<233){
     			$icon=-90;
     		}elseif($icon>299&&$icon<322){
     			$icon=-60;
     		}elseif($icon>499&&$icon<532){
     			$icon=-60;
     		}elseif($icon>599&&$icon<623){
     			$icon=-120;
     		}elseif($icon>700&&$icon<782){
     			$icon=-150;
     		}elseif($icon==800){
     			$icon=0;
     		}elseif($icon>800&&$icon<805){
     			$icon=-30;
     		}
     		$iconurl='/img/index/weather_icons.png';
				
		    return array("temp"=>$temp,"weather_icon"=>$icon,"weather_icon_url"=>$iconurl);
    	} else {return NULL;}
    
    }
}
